package shop.up.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shop.up.common.result.Result;
import shop.up.model.product.BaseSaleAttr;
import shop.up.model.product.SpuInfo;
import shop.up.model.product.SpuSaleAttr;
import shop.up.product.service.ManageService;

import java.util.List;

/**
 * @author zhh
 * @date 2023/3/29 14:15
 */
@Api(tags = "spu 信息接口")
@RestController
@RequestMapping("admin/product")
public class SpuManageController {
    // 引入服务层
    @Autowired
    private ManageService manageService;

    /**
     * 根据spuId 查询销售属性集合
     * @param spuId
     * @return
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 保存spu
     * @param spuInfo
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        // 调用服务层的保存方法
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @ApiOperation("获取品牌信息")
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo) {
        // 创建一个Page对象
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);
        // 获取数据
        IPage<SpuInfo> spuInfoIPageList = manageService.getSpuInfoPage(spuInfoPage, spuInfo);
        // 将获取到的数据返回即可
        return Result.ok(spuInfoIPageList);
    }

    // 销售属性  http://api.up.shop/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    @ApiOperation(value = "查询所有的销售属性集合")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
}
